*------------------------------------------------------------------------------*
							*** ESTIMATION SAMPLE ***
*------------------------------------------------------------------------------*

use "${tempdata}kreise_complete_all.dta", clear

*------------------------------------------------------------------------------*

***Prepare data

xtset kreis_code

encode election_typeBWorLW, gen(type)


**Macros
forval i = 1/30 {
	//PM10
	local PM10_lags "`PM10_lags' lag_PM10_ug_`i'"
	local PM10_leads "`PM10_leads' lead_PM10_ug_`i'"
	local PM10_10ug_lags "`PM10_10ug_lags' lag_PM10_10ug_`i'_30km"
	local PM10_10ug_leads "`PM10_10ug_leads' lead_PM10_10ug_`i'_30km"
	//Ozone
	local O3_lags "`O3_lags' lag_O3_ug_`i'"
	local O3_leads "`O3_leads' lead_O3_ug_`i'"
	local O3_10ug_lags "`O3_10ug_lags' lag_O3_10ug_`i'_30km"
	local O3_10ug_leads "`O3_10ug_leads' lead_O3_10ug_`i'_30km"
	//Temperature
	local temp_lags "`temp_lags' lag_temp_`i'_30km"
	local temp_leads "`temp_leads' lead_temp_`i'_30km"
	//Huminidy
	local humid_rel_lags "`humid_rel_lags' lag_humid_rel_`i'_30km"
	local humid_rel_leads "`humid_rel_leads' lead_humid_rel_`i'_30km"
	//Precipitations
	local precip_lags "`precip_lags' lag_precip_`i'_30km"
	local precip_leads "`precip_leads' lead_precip_`i'_30km"
	//Wind speed
	local wind_speed_lags "`wind_speed_lags' lag_wind_speed_`i'_30km"
	local wind_speed_leads "`wind_speed_leads' lead_wind_speed_`i'_30km"
} // i  
local radius " 20 30 40 50 "
local poll_weather_lagsleads " `PM10_10ug_lags' `PM10_10ug_leads' `O3_10ug_lags' `O3_10ug_leads' `temp_lags' `temp_leads' `humid_rel_lags' `humid_rel_leads' `precip_lags' `precip_leads' `wind_speed_lags' `wind_speed_leads' "


**Generate PM10, O3 and NO2 in 10s of micrograms
gen PM10_30km_10ug = PM10_ug_weight_30km / 10
gen O3_30km_10ug = O3_ug_weight_30km / 10
gen NO2_30km_10ug = NO2_ug_weight_30km /10


**Generate lags and leads of PM10 and Ozone in 10s of micrograms
foreach r of local radius{
	forval t=1/30 {
	//PM10
	gen lag_PM10_10ug_`t'_`r'km = lag_PM10_ug_`t'_weight_`r'km / 10
	gen lead_PM10_10ug_`t'_`r'km = lead_PM10_ug_`t'_weight_`r'km / 10
	//Ozone
	gen lag_O3_10ug_`t'_`r'km = lag_O3_ug_`t'_weight_`r'km / 10
	gen lead_O3_10ug_`t'_`r'km = lead_O3_ug_`t'_weight_`r'km / 10
	} // t
} // r


**Label variables
foreach r of local radius {
	label variable PM10_ug_weight_`r'km "PM10_`r'km"
	label variable PM2_5_mg_weight_`r'km "PM2.5_`r'km"
	label variable O3_ug_weight_`r'km "O3_`r'km"
} // r
label variable PM10_30km_10ug "PM10_30km (10s of ug)"
label variable O3_30km_10ug "O3_30km (10s of ug)"


**Rename variable
foreach r of local radius {
	rename PM10_ug_weight_`r'km PM10_`r'km
	rename PM2_5_mg_weight_`r'km PM2_5_`r'km
	rename O3_ug_weight_`r'km O3_`r'km
	rename temp_weight_`r'km temp_`r'km
	rename humid_rel_weight_`r'km humid_rel_`r'km 
	rename wind_speed_weight_`r'km wind_speed_`r'km
	rename precip_weight_`r'km precip_`r'km
	forval t=1/30 {
			rename lag_PM10_ug_`t'_weight_`r'km lag_PM10_`t'_`r'km
			rename lead_PM10_ug_`t'_weight_`r'km lead_PM10_`t'_`r'km
		} // t
	foreach x in O3_ug temp humid_rel wind_speed precip {
		forval t=1/30 {
			rename lag_`x'_`t'_weight_`r'km lag_`x'_`t'_`r'km
			rename lead_`x'_`t'_weight_`r'km lead_`x'_`t'_`r'km
		} // t
	} // x
} // r

**Drop lags and leads for radiuses != 30
foreach r in 20 40 50 {
	forval t=1/30 {
		drop lag_PM10_10ug_`t'_`r'km lead_PM10_10ug_`t'_`r'km lag_O3_10ug_`t'_`r'km lead_O3_10ug_`t'_`r'km lag_PM10_`t'_`r'km lead_PM10_`t'_`r'km
		foreach x in O3_ug temp humid_rel wind_speed precip {
			drop lag_`x'_`t'_`r'km lead_`x'_`t'_`r'km
		} // x
	} // t
	forval s=1/5 {
		drop lag_PM10_ug_s`s'_weight_`r'km
	} // s
} // r

*------------------------------------------------------------------------------*

***Generate different estimation samples
*All controls
qui reghdfe PM10_30km_10ug O3_30km_10ug temp_30km humid_rel_30km precip_30km wind_speed_30km ///
	turnout Incumbent_share Est_opp_share pop_tot GDP_capita employment i.type, ${hdfe_opt}
gen samplePM10_allcontrols=e(sample)


*All controls + lags and leads of PM10
qui reghdfe PM10_30km_10ug O3_30km_10ug temp_30km humid_rel_30km precip_30km wind_speed_30km ///
	lag_PM10_10ug_1_30km lag_PM10_10ug_2_30km lag_PM10_10ug_3_30km lag_PM10_10ug_4_30km lag_PM10_10ug_5_30km lag_PM10_10ug_6_30km lag_PM10_10ug_7_30km ///
	lead_PM10_10ug_1_30km lead_PM10_10ug_2_30km lead_PM10_10ug_3_30km lead_PM10_10ug_4_30km lead_PM10_10ug_5_30km lead_PM10_10ug_6_30km lead_PM10_10ug_7_30km ///
	turnout Incumbent_share Est_opp_share pop_tot GDP_capita employment i.type, ${hdfe_opt}
gen samplePM10_ll_allcontrols=e(sample)

*All controls + lags and leads of O3 and weather
qui reghdfe PM10_30km_10ug O3_30km_10ug temp_30km humid_rel_30km precip_30km wind_speed_30km  ///
	lag_PM10_10ug_1_30km lag_PM10_10ug_2_30km lag_PM10_10ug_3_30km lag_PM10_10ug_4_30km lag_PM10_10ug_5_30km lead_PM10_10ug_1_30km lead_PM10_10ug_2_30km lead_PM10_10ug_3_30km lead_PM10_10ug_4_30km lead_PM10_10ug_5_30km  ///
	turnout Incumbent_share Est_opp_share pop_tot GDP_capita employment  ///
	lag_O3_10ug_1_30km lag_O3_10ug_2_30km lag_O3_10ug_3_30km lag_O3_10ug_4_30km lag_O3_10ug_5_30km lead_O3_10ug_1_30km lead_O3_10ug_2_30km lead_O3_10ug_3_30km lead_O3_10ug_4_30km lead_O3_10ug_5_30km  ///
	lag_temp_1_30km lag_temp_2_30km lag_temp_3_30km lag_temp_4_30km lag_temp_5_30km lead_temp_1_30km lead_temp_2_30km lead_temp_3_30km lead_temp_4_30km lead_temp_5_30km  ///
	lag_humid_rel_1_30km lag_humid_rel_2_30km lag_humid_rel_3_30km lag_humid_rel_4_30km lag_humid_rel_5_30km lead_humid_rel_1_30km lead_humid_rel_2_30km lead_humid_rel_3_30km lead_humid_rel_4_30km lead_humid_rel_5_30km  ///
	lag_precip_1_30km lag_precip_2_30km lag_precip_3_30km lag_precip_4_30km lag_precip_5_30km lead_precip_1_30km lead_precip_2_30km lead_precip_3_30km lead_precip_4_30km lead_precip_5_30km  ///
	lag_wind_speed_1_30km lag_wind_speed_2_30km lag_wind_speed_3_30km lag_wind_speed_4_30km lag_wind_speed_5_30km lead_wind_speed_1_30km lead_wind_speed_2_30km lead_wind_speed_3_30km lead_wind_speed_4_30km lead_wind_speed_5_30km  ///
	i.type, ${hdfe_opt}
gen samplePM10_ll_o3w_allcontrols=e(sample)

*All controls + 30 PM10 lags leads
qui reghdfe PM10_30km_10ug O3_30km_10ug temp_30km humid_rel_30km precip_30km wind_speed_30km `poll_weather_lagsleads' turnout Incumbent_share Est_opp_share pop_tot GDP_capita employment i.type, ${hdfe_opt}
gen samplePM10_28ll_allcontrols=e(sample)

*------------------------------------------------------------------------------*

*Save
qui compress
save "${tempdata}kreise_complete_all.dta", replace

*------------------------------------------------------------------------------*

clear

exit
